package com.example.forum.services.impl;

import com.example.forum.commen.AppResult;
import com.example.forum.commen.ResultCode;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import com.example.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private IArticleService articleService;
    @Override
    public void create(ArticleReply articleReply) {
        if(articleReply == null || articleReply.getArticleId() == null
            || articleReply.getPostUserId() == null
            || StringUtil.isEmpty(articleReply.getContent())) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte)0);
        articleReply.setDeleteState((byte)0);
        articleReply.setUpdateTime(new Date());
        articleReply.setCreateTime(new Date());
        //写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新贴子表的回复数
        articleService.addOneArticleReplayCountById(articleReply.getArticleId());
        log.info("回复成功 article id = "+articleReply.getArticleId()+" replay id = "+articleReply.getReplyId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        /*非空校验*/
        if(articleId == null || articleId < 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao
        List<ArticleReply> list = articleReplyMapper.selectByArticleId(articleId);
        return list;
    }
}
